package com._9xcloud.leaverequest.service;

import com._9xcloud.leaverequest.domain.Clazz;
import com._9xcloud.leaverequest.domain.College;
import com._9xcloud.leaverequest.domain.Teacher;
import com._9xcloud.leaverequest.repository.ClazzRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2015/5/28.
 */
@Repository
@Transactional
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private ClazzRepository clazzRepository;

    public Clazz create(String name, int enrollYear, College college, Teacher teacher) {
        Clazz clazz = new Clazz(name, enrollYear, college, teacher);
        return clazzRepository.saveAndFlush(clazz);
    }

    public void removeAll() {
        clazzRepository.deleteAll();
        clazzRepository.flush();
    }

    @Transactional(readOnly = true)
    public List<Clazz> getAll() {
        return clazzRepository.findAll();
    }

    @Transactional(readOnly = true)
    public Set<Clazz> getByTeacherId(int teacherId) {
        Teacher teacher = teacherService.getById(teacherId);
        return teacher.getClazzs();
    }

}
